package com.gwclh.personnel.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.common.constants.UserConstants;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.User;
import com.gwclh.personnel.mapper.UserMapper;
import com.gwclh.personnel.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.vo.uservo.BaseUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户表，用户由超级管理员添加，使用用户名、密码登录 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页查询用户信息
     * @param pageDomain 分页参数
     * @param user 查询条件
     * @return
     */
    @Override
    public ResultVo listUsers(PageDomain pageDomain, User user) {
        Page<User> userPage = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        userPage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("username",user.getUsername())
                .like("real_name",user.getRealName());

        if(!StrUtil.isEmpty(user.getRole())){
            userQueryWrapper.eq("role",user.getRole());
        }
        if(user.getStatus()!=null){
            userQueryWrapper.eq("status",user.getStatus());
        }

        this.page(userPage,userQueryWrapper);
        // vo 转换
        List<BaseUserVo> baseUserVos = new ArrayList<>();
        userPage.getRecords().forEach(user1 -> {
            BaseUserVo baseUserVo = new BaseUserVo();
            BeanUtils.copyProperties(user1,baseUserVo);
            baseUserVos.add(baseUserVo);
        });

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("users",baseUserVos);
        resultMap.put("total",userPage.getTotal());
        return ResultVo.success(resultMap);
    }

    /**
     * 添加用户
     * @param user 前端传递的user对象
     * @return
     */
    @Override
    public ResultVo insertUser(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",user.getUsername());
        Assert.isNull(this.getOne(userQueryWrapper),"该用户已存在");

        user.setPassword(SecureUtil.md5(user.getPassword()));
        user.setStatus(true);
        Assert.isTrue(this.save(user),"添加用户失败");
        return ResultVo.success();
    }

    // 根据id删除redis中的用户信息,
    @Override
    public void delRedisUser(Integer id) {
        // 查询当前redis中是否用该用户的信息
        User user = (User) redisTemplate.opsForValue().get(UserConstants.SHIRO_USER_REDIS_KEY+id);
        if(user!=null){
            // 删除该用户的redis信息
            redisTemplate.delete(UserConstants.SHIRO_USER_REDIS_KEY+id);
        }
    }
}
